package com.frxuexi.video

import androidx.lifecycle.MutableLiveData
import com.frxuexi.lib_base.db.SearchDao
import com.frxuexi.lib_base.entity.CommentBean
import com.frxuexi.lib_base.entity.SearchBean
import com.frxuexi.lib_base.entity.VideoBean
import com.frxuexi.lib_base.entity.VideoBeanItem
import com.frxuexi.lib_base.mvvm.BaseViewModel
import com.frxuexi.lib_base.net.VideoApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import javax.inject.Inject

@HiltViewModel
class VideoViewModel @Inject constructor(val apiService: VideoApiService,val searchDao: SearchDao):BaseViewModel(){
    var videoSuc = MutableStateFlow<ArrayList<VideoBeanItem>>(ArrayList<VideoBeanItem>())

    var searchSuc = MutableLiveData<VideoBean>()

    var addSearchSuc = MutableLiveData<Long>()

    var querySearchSuc = MutableLiveData<List<SearchBean>>()

    var deleteSearchSuc = MutableLiveData<Int>()

    var addCommentSuc = MutableLiveData<CommentBean>()

    var zanSuc = MutableLiveData<String>()

    var noZanSuc = MutableLiveData<String>()

    var getCommentSuc = MutableLiveData<CommentBean>()

    fun getVideos(page:Int){
        httpRequestShop({apiService.getVideos(page)},{videoSuc.value=it})
    }

    fun searchVideos(key:String){
        httpRequestShop({apiService.searchVideo(key)},searchSuc::postValue)
    }

    fun addSearch(searchBean: SearchBean){
        roomRequest({searchDao.addSearch(searchBean)},addSearchSuc::postValue)
    }

    fun querySearch(){
        roomRequest({searchDao.querySearch()},querySearchSuc::postValue)
    }

    fun deleteSearch(searchBean: SearchBean){
        roomRequest({searchDao.deleteSearch(searchBean)},deleteSearchSuc::postValue)
    }

    fun addComment(map: Map<String,Any>){
        httpRequestShop({apiService.addComment(mapToBody(map))},addCommentSuc::postValue)
    }

    fun zan(id:Int){
        httpRequestShop({apiService.zan(id)},zanSuc::postValue)
    }

    fun noZan(id:Int){
        httpRequestShop({apiService.nozan(id)},noZanSuc::postValue)
    }

    fun getComments(vid:Int){
        httpRequestShop({apiService.getComments(vid)},getCommentSuc::postValue)
    }

}